package com.liu.susu.thread.task.async.async1;

import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.framework.AopContext;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * @FileName AsyncTask1
 * @Description
 * @Author susu
 * @date 2022-03-09
 **/
@Service
@Component
@Slf4j
@Transactional(value = "transactionManager", readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
public class AsyncTask1 {

    @Autowired
    private AsyncTask2 asyncTask2;

    @Autowired
    private ApplicationContext applicationContext;

    public String doMyAllTask() {
        log.info(Thread.currentThread().getName()+"-->begin doMyAllTask()...");
        long currentTimeMillis = System.currentTimeMillis();
//        doMyTask1();
//        doMyTask2();
//        doMyTask3();

//        asyncTask2.doMyTask1();
//        asyncTask2.doMyTask2();
//        asyncTask2.doMyTask3();

        AsyncTask1 asyncTask1 = applicationContext.getBean(AsyncTask1.class);
//        asyncTask1.doMyTask1();
//        asyncTask1.doMyTask2();
//        asyncTask1.doMyTask3();

        //////////////////////////////////////////////
        boolean isAop = AopUtils.isAopProxy(AsyncTask1.class);//是否是代理对象；
        boolean isCglib = AopUtils.isCglibProxy(AsyncTask1.class);  //是否是CGLIB方式的代理对象；
        boolean isJdk = AopUtils.isJdkDynamicProxy(AsyncTask1.class);  //是否是JDK动态代理方式的代理对象；

        AsyncTask1 asyncTask1Proxy = (AsyncTask1) AopContext.currentProxy();
        log.info("asyncTask1 == asyncTask1Proxy===>"+(asyncTask1 == asyncTask1Proxy));
        asyncTask1Proxy.doMyTask1();
        asyncTask1Proxy.doMyTask2();
        asyncTask1Proxy.doMyTask3();

        long currentTimeMillis1 = System.currentTimeMillis();
        log.info("end doMyAllTask()-->doMyAllTask任务总耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms");
        return "ok";
    }

    @Async
    public void doMyTask1() {
        log.info(Thread.currentThread().getName()+"-->begin doMyTask1()...");

        long currentTimeMillis = System.currentTimeMillis();
        try {
            Thread.sleep(2200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long currentTimeMillis1 = System.currentTimeMillis();

        log.info("end doMyTask1()-->doMyTask1任务耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms");
    }

    @Async
    public void doMyTask2() {
        log.info(Thread.currentThread().getName()+"-->begin doMyTask2()...");

        long currentTimeMillis = System.currentTimeMillis();
        try {
            Thread.sleep(3300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long currentTimeMillis1 = System.currentTimeMillis();

        log.info("end doMyTask2()-->doMyTask2任务耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms");
    }
    @Async
    public void doMyTask3() {
        log.info(Thread.currentThread().getName()+"-->begin doMyTask3()...");

        long currentTimeMillis = System.currentTimeMillis();
        try {
            Thread.sleep(1100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long currentTimeMillis1 = System.currentTimeMillis();

        log.info("end doMyTask3()-->doMyTask3任务耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms");
    }


}
